package com.zksk.admin.controller.customer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.domain.customer.vo.CustomerFVo;
import com.zksk.admin.domain.customer.vo.CustomerProdReqFVo;
import com.zksk.admin.domain.customer.vo.CustomerProdReqVo;
import com.zksk.admin.domain.customer.vo.CustomerVo;
import com.zksk.admin.service.customer.CustomerProdReqService;
import com.zksk.admin.service.customer.CustomerService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.Customer;
import com.zksk.mbg.admin.domain.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther xiebo
 * @date 2022/12/2
 **/
@Slf4j
@RestController
@RequestMapping("/customer/")
@Api(tags = "客户相关接口")
public class CustomerController extends BaseController
{

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerProdReqService customerProdReqService;

    @PostMapping("selectList")
    @Log(title = "查询客户信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "分页条件查询客户信息")
    public R<Page<CustomerVo>> selectList(@RequestBody CustomerFVo customerFVo)
    {
        return customerService.selectList(customerFVo);
    }

    @Log(title = "客户信息查询", businessType = BusinessType.SELECT)
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据ID查询产品需求信息")
    public R<CustomerVo> getById(@PathVariable Long id)
    {
        CustomerVo customerVo = new CustomerVo();
        Customer customer = customerService.getById(id);
        BeanUtil.copyProperties(customer,customerVo);
        CustomerProdReqFVo customerProdReqFVo = new CustomerProdReqFVo();
        customerProdReqFVo
                .setCustomerId(id)
                .setPageNum(1)
                .setPageSize(1000);
        customerVo.setPageList(customerProdReqService.selectList(customerProdReqFVo).getData());
        return R.ok(customerVo);
    }


    @GetMapping("selectNameList/{name}")
    @Log(title = "查询客户信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "模糊查询客户名称,返回5条记录")
    public R<Page<CustomerVo>> selectNameList(@PathVariable("name") String name)
    {

        Page<CustomerVo> voPage = new Page<>();
        Page<Customer> page = customerService.page(new Page<>(1, 5),
                new LambdaQueryWrapper<Customer>()
                        .like(StrUtil.isEmpty(name), Customer::getCustomerName, name));
        //实体转Vo
        BeanUtil.copyProperties(page,voPage);
        return R.ok(voPage);
    }

    @PutMapping("update")
    @Log(title = "修改客户信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改客户基本信息")
    public R update(@RequestBody @Validated(Update.class) CustomerVo customerVo)
    {
        if (CollUtil.isEmpty(customerVo.getBindIds()))
        {
            return R.fail("产品需求未添加!");
        }

        return customerService.updateCustomer(customerVo);
    }


    //    @RequiresPermissions("platform:customer:list")
    @PostMapping("add")
    @Log(title = "新增客户信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增客户基本信息")
    public R add(@RequestBody @Validated(Insert.class) CustomerVo customerVo)
    {
        if (customerService.checkUserNameUnique(customerVo.getCustomerName()))
        {
            return R.fail("客户名称已存在！");
        }
        else if (customerService.checkContacts(customerVo.getContacts()))
        {
            return R.fail("联系人已存在！");
        }
        else if (customerService.checkContactsPhone(customerVo.getContactsPhone()))
        {
            return R.fail("联系人电话已存在！");
        }
        else if (CollUtil.isEmpty(customerVo.getBindIds()))
        {
            return R.fail("产品需求未添加!");
        }
        return customerService.insert(customerVo);
    }

    @DeleteMapping("delete/{ids}")
    @Log(title = "删除客户信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除客户(逻辑删除)")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@PathVariable("ids") List<Long> ids)
    {
        customerService.removeBatchByIds(ids);
        //TODO 调用产品关联的删除产品关联接口
        return R.ok("删除成功!");
    }

}
